package com.heima.article.controller.v1;

import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/author")
@Api(tags = "作者管理模块")
public class AuthorController implements AuthorControllerApi {

    @Autowired
    private AuthorService authorService;
    /**
     * 根据id查询作者信息
     *
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/findByUserId/{userId}")
    @ApiOperation(value = "根据id查询作者")
    public ApAuthor findByUserId(@PathVariable("userId") Integer userId) {
        return authorService.getById(userId);
        //return authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
    }

    /**
     * 保存作者数据
     *
     * @param apAuthor
     * @return
     */
    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存作者信息")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        authorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据Id查询作者
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }
}
